﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace Login
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //doorgegeven parameters
            String name = txtUsername.Text;
            String pass = txtPassword.Text;

            //login = waar -> doorsturen naar loadbar
            if (loginCheck(name, pass) == true)
            {
                this.Dispose(false);
                Loadscreen.Loadscreen loading = new Loadscreen.Loadscreen(name, pass);
                loading.Show();
            }
            //login = false -> doorsturen naar error screen
            else
            {
                MessageBox.Show("Failed to log in");
            }
        }

        public Boolean loginCheck(string email, string wachtwoord)
        {
            //methode die checkt of de gebruiker wel / niet in mag loggen
            string username = email;
            string password = wachtwoord;
            
            try
            {
                //connectie naar server + query
                MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
                MySqlCommand login = new MySqlCommand("SELECT Email, Wachtwoord FROM confirmed_users WHERE Email='" + username + "' AND Wachtwoord='" + password + "'", grabConnection);
                grabConnection.Open();

                //data lezen
                MySqlDataReader reader = login.ExecuteReader();

                if (reader.Read() != false)
                {
                    if (reader.IsDBNull(0) == true)
                    {
                        //alles sluiten, login is gefaald
                        grabConnection.Close();
                        reader.Dispose();
                        login.Dispose();
                        grabConnection.Close();
                        return false;
                    }
                    else
                    {
                        //login geslaagd!
                        grabConnection.Close();
                        reader.Dispose();
                        login.Dispose();
                        grabConnection.Close();
                        return true;
                    }
                }
                else
                {
                    grabConnection.Close();
                    return false;
                }  
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }
        }
    }
}

